package com.baibei.module.stock;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.baibei.basic.module.utils.ViewExtCompat;


/**
 * 股票图表布局
 * Created by ChenRui on 2017/6/12 0012 10:52.
 */
public class StockLayout extends LinearLayout {

    // 是否开市
    private boolean mIsTrade = true;
    // 空视图的每一条线的距离
    private int mLinesMargin = 40;
    //  空视图的每一条线的高度
    private float mLinesHeight = 0.5f;
    private Paint mLinesPaint;
    private int mLineColor;
    private View mLoadingLayout; // 加载中对话框
    private View mEmptyLayout; // 空视图

    public StockLayout(Context context) {
        super(context);
        init();
    }

    public StockLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public StockLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    protected void init() {
        // 添加视图
        LayoutInflater.from(getContext()).inflate(R.layout.layout_stock_loading, this, true);
        mEmptyLayout = LayoutInflater.from(getContext()).inflate(R.layout.layout_stock_empty, this, false);
        this.addView(mEmptyLayout);

        mLoadingLayout = findViewById(R.id.ll_stock_loading);
        mLinesMargin = (int) ViewExtCompat.px2dp(getContext(), mLinesMargin);
        mLinesHeight = (int) ViewExtCompat.px2dp(getContext(), mLinesHeight);

        mLineColor = Color.parseColor("#EBEBEB");
        mLinesPaint = new Paint();
        mLinesPaint.setAntiAlias(true);
        mLinesPaint.setColor(mLineColor);

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 画休市状态的线
        if (!mIsTrade) {
            drawLines(canvas);
        }
    }

    /**
     * 设置是否为开休市状态
     *
     * @param isTrade true = 开市，false = 休市
     */
    public void setIsTrade(boolean isTrade) {
        mIsTrade = isTrade;
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View view = getChildAt(i);
            if (view == mLoadingLayout) {
                continue;
            }
            view.setVisibility(isTrade ? view.getVisibility() : View.GONE);
        }

        mEmptyLayout.setVisibility(isTrade ? mEmptyLayout.getVisibility() : View.GONE);
        mLinesPaint.setColor(mIsTrade ? Color.TRANSPARENT : mLineColor);
        invalidate();
    }

    /**
     * 设置是否为空视图
     */
    public void setEmpty(boolean isEmpty) {
        int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View view = getChildAt(i);
            if (view == mLoadingLayout) {
                continue;
            }
            view.setVisibility(isEmpty ? View.GONE : View.VISIBLE);
        }
        mEmptyLayout.setVisibility(isEmpty ? View.VISIBLE : View.GONE);
        invalidate();
    }


    /**
     * 画空视图
     */
    private void drawLines(Canvas canvas) {
        // 1、根据高度，计算每一条线的距离
        int height = canvas.getHeight();
        int width = canvas.getWidth();
        int count = Math.max(height / mLinesMargin, 0);
        for (int i = 1; i < count; i++) {
            float y = mLinesMargin * i;
            canvas.drawRect(0, y, width, y + mLinesHeight, mLinesPaint);
        }
    }

    public void dismissLoading() {
        if (mLoadingLayout != null) {
            mLoadingLayout.setVisibility(View.GONE);
        }
    }

    public void showLoading() {
        if (mLoadingLayout != null) {
            mLoadingLayout.setVisibility(View.VISIBLE);
        }
    }
}
